#include "WheelMotor.h"

// 构造函数
WheelMotor::WheelMotor(String motorNameV, PinType forwardPinV, PinType reversePinV) {
    motorName = motorNameV;
    forwardPin = forwardPinV;
    reversePin = reversePinV;

    // 初始化PWM通道
    ledcAttach(forwardPin, FREQ_PWM, RESOLUTION_PWM);
    ledcAttach(reversePin, FREQ_PWM, RESOLUTION_PWM);

    // 停止电机
    ledcWrite(forwardPin, 0);
    ledcWrite(reversePin, 0);
}

// 启动车轮电机
OperateStatus WheelMotor::operateMotor(DirectionType direction, int duty) {
    if (duty == 0 || direction == STOP) {
        ledcWrite(forwardPin, 0);
        ledcWrite(reversePin, 0);
        return SUCCESS;
    }

    ledcWrite(forwardPin, direction == FORWARD ? duty : 0);
    ledcWrite(reversePin, direction == REVERSE ? duty : 0);

    return SUCCESS;
}
